home *** CD-ROM | disk | FTP | other *** search
-
- package sub_arctic.input;
-
- import sub_arctic.lib.interactor;
- import sub_arctic.lib.top_level;
-
- import java.awt.Point;
- import java.awt.Event;
-
- /**
- * This class represents an input event. It is a wrapper around
- * java.awt.Event and encapsulates its public instance variables (providing
- * access methods instead). In general for each public instance variable "v"
- * in the original Event class there are access methods "v()" and "set_v()"
- * in this class. One exception is that the x and y fields have been renamed
- * global_x and global_y to better reflect their meaning in subArctic. In
- * addition, this class introduces fields to maintain position in an object's
- * local coordinate system and to record the root top_level object along with
- * methods to manipulate these.
- *
- * @see java.awt.Event
- * @author Scott Hudson
- */
- public class event {
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** The AWT event object being wrapped */
- protected Event _awt_event;
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** AWT field: target component. */
- public Object target() {return _awt_event.target;}
-
- /** Set AWT field: target component. */
- public void set_target(Object o) {_awt_event.target = o;}
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** AWT field: time stamp. */
- public long when() {return _awt_event.when;}
-
- /** Set AWT field: time stamp. */
- public void set_when(long w) {_awt_event.when = w;}
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** AWT field: type of this event. */
- public int id() {return _awt_event.id;}
-
- /** Set AWT field: type of this event. */
- public void set_id(int i) {_awt_event.id = i;}
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** AWT field: x coordinate of the event. In our case this is the
- * global position (position in the top_level root object). */
- public int global_x() {return _awt_event.x;}
-
- /** Set AWT field: x coordinate of the event. In our case this is the
- * global position (position in the top_level root object). */
- public void set_global_x(int xv) {_awt_event.x = xv;}
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** AWT field: y coordinate of the event. In our case this is the
- * global position (position in the top_level root object). */
- public int global_y() {return _awt_event.y;}
-
- /** Set AWT field: y coordinate of the event. In our case this is the
- * global position (position in the top_level root object). */
- public void set_global_y(int yv) {_awt_event.y = yv;}
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** AWT field: key that was pressed in a keyboard event. */
- public int key() {return _awt_event.key;}
-
- /** Set AWT field: key that was pressed in a keyboard event. */
- public void set_key(int k) {_awt_event.key = k;}
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** AWT field: state of the modifier keys. */
- public int modifiers() {return _awt_event.modifiers;}
-
- /** Set AWT field: state of the modifier keys. */
- public void set_modifiers(int m) {_awt_event.modifiers = m;}
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** AWT field: The number of consecutive clicks. This field is relevant
- * only for MOUSE_DOWN events. If the field isn't set it will be 0.
- * Otherwise, it will be 1 for single-clicks, 2 for double-clicks, and so on.
- */
- public int clickCount() {return _awt_event.clickCount;}
-
- /** Set AWT field: The number of consecutive clicks. This field is relevant
- * only for MOUSE_DOWN events. If the field isn't set it will be 0.
- * Otherwise, it will be 1 for single-clicks, 2 for double-clicks, and so on.
- */
- public void set_clickCount(int c) {_awt_event.clickCount = c;}
-
- /** Alias for clickCount() using subArctic style name. */
- public int click_count() {return clickCount();}
-
- /** Alias for set_clickCount() using subArctic style name. */
- public void set_click_count(int c) {set_clickCount(c);}
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** AWT field: An arbitrary argument. */
- public Object arg() {return _awt_event.arg;}
-
- /** set AWT field: An arbitrary argument. */
- public void set_arg(Object a) {_awt_event.arg = a;}
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** AWT field: The next event. Used when putting events into a linked list. */
- public Event evt() {return _awt_event.evt;}
-
- /** AWT field: The next event. Used when putting events into a linked list. */
- public void evt(Event e) {_awt_event.evt = e;}
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** X position in local coordinates of the object it is delivered to.
- * The global_x field will retain the global coordinate value. */
- protected int _local_x;
-
- /** X position in local coordinates of the object it is delivered to.
- * The global_x field will retain the global coordinate value. */
- public int local_x() {return _local_x;}
-
- /** Set the X position in local coordinates of the object it is delivered to.
- * The global_x field will retain the global coordinate value. */
- public void set_local_x(int x) {_local_x = x;}
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Y position in local coordinates of the object it is delivered to.
- * The global_y field will retain the global coordinate value. */
- protected int _local_y;
-
- /** Y position in local coordinates of the object it is delivered to.
- * The global_y field will retain the global coordinate value. */
- public int local_y() {return _local_y;}
-
- /** Set the Y position in local coordinates of the object it is delivered to.
- * The global_y field will retain the global coordinate value. */
- public void set_local_y(int y) {_local_y = y;}
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Top level interactor that this event occured in. Global coordinates
- * are with respect to the origin of that object. */
- protected top_level _root_interactor;
-
- /** Top level interactor that this event occured in. Global coordinates
- * are with respect to the origin of that object. */
- public top_level root_interactor() {return _root_interactor;}
-
- /** Set the top level interactor that this event occured in. Global
- * coordinates are with respect to the origin of that object. Note, this
- * does not update coordinates for you and in general needs to be used
- * with great care.
- */
- public void set_root_interactor(top_level t) {_root_interactor = t;}
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Construct one of these events from a normal AWT event.
- * @param Event other the AWT event we are creating this from.
- * @param top_level root the subArctic root object that this event is
- * associated with (this provides its global
- * coordinate system).
- */
- public event(Event other, top_level root)
- {
- /* Initialize a new awt event from the other */
- _awt_event = new Event(other.target, other.when, other.id,
- other.x, other.y, other.key, other.modifiers, other.arg);
-
- /* plus the part they don't let you send to the constructor */
- _awt_event.clickCount = other.clickCount;
-
- /* plus our additions -- start out with local == global */
- _local_x = other.x;
- _local_y = other.y;
-
- _root_interactor = root;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Copy constructor.
- * @param event other the event we are making a copy of.
- */
- public event(event other)
- {
- this(other._awt_event, other.root_interactor());
- _local_x = other._local_x;
- _local_y = other._local_y;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Change event which is currently placed in coords of the parent of the
- * given object into one placed in the local coords of the given object.
- * This only works if we know the coordinate system of the event in advance.
- * If this is now know, use global_to_local() instead.<p>
- *
- * @see sub_arctic.input.event#global_to_local
- * @interactor of_obj the interactor object in question.
- */
- public void into_local(interactor of_obj)
- {
- Point local_pt = of_obj.into_local(new Point(local_x(),local_y()));
- set_local_x(local_pt.x);
- set_local_y(local_pt.y);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Change event which is currently placed in the given object's local
- * coords into one placed in the coords of its parent.
- * This only works if we know the coordinate system of the event in advance.
- * If this is not know, use reset_to_global() to get the event into a
- * known coordinate system.
- * <p>
- *
- * @see sub_arctic.input.event#into_local
- * @see sub_arctic.input.event#global_to_local
- * @see sub_arctic.input.event#reset_to_global
- * @interactor of_obj the interactor object in question.
- */
- public void into_parents(interactor of_obj)
- {
- Point local_pt = of_obj.into_parent(new Point(local_x(),local_y()));
- set_local_x(local_pt.x);
- set_local_y(local_pt.y);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Put event into the given object's local coordinates regardless of what
- * coordinates it is in now. */
- public void global_to_local(interactor of_obj)
- {
- Point local_pt = of_obj.global_to_local(new Point(global_x(),global_y()));
- set_local_x(local_pt.x);
- set_local_y(local_pt.y);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Force event back into global coordinates */
- public void reset_to_global()
- {
- set_local_x(global_x());
- set_local_y(global_y());
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Create a human readable string corresponding to an event id code.
- * @param int id the event id code.
- * @return String a string with the human readable name of the event type.
- */
- public static String id_string(int id)
- {
- switch (id) {
- case Event.ACTION_EVENT: return "ACTION_EVENT";
- case Event.GOT_FOCUS: return "GOT_FOCUS";
- case Event.KEY_ACTION: return "KEY_ACTION";
- case Event.KEY_ACTION_RELEASE: return "KEY_ACTION_RELEASE";
- case Event.KEY_PRESS: return "KEY_PRESS";
- case Event.KEY_RELEASE: return "KEY_RELEASE";
- case Event.LIST_DESELECT: return "LIST_DESLECT";
- case Event.LIST_SELECT: return "LIST_SELECT";
- case Event.LOAD_FILE: return "LOAD_FILE";
- case Event.LOST_FOCUS: return "LOST_FOCUS";
- case Event.MOUSE_DOWN: return "MOUSE_DOWN";
- case Event.MOUSE_DRAG: return "MOUSE_DRAG";
- case Event.MOUSE_ENTER: return "MOUSE_ENTER";
- case Event.MOUSE_EXIT: return "MOUSE_EXIT";
- case Event.MOUSE_MOVE: return "MOUSE_MOVE";
- case Event.MOUSE_UP: return "MOUSE_UP";
- case Event.SAVE_FILE: return "SAVE_FILE";
- case Event.SCROLL_ABSOLUTE: return "SCROLL_ABSOLUTE";
- case Event.SCROLL_LINE_DOWN: return "SCROLL_LINE_DOWN";
- case Event.SCROLL_LINE_UP: return "SCROLL_LINE_UP";
- case Event.SCROLL_PAGE_DOWN: return "SCROLL_PAGE_DOWN";
- case Event.SCROLL_PAGE_UP: return "SCROLL_PAGE_UP";
- case Event.WINDOW_DEICONIFY: return "WINDOW_DEICONIFY";
- case Event.WINDOW_DESTROY: return "WINDOW_DESTROY";
- case Event.WINDOW_EXPOSE: return "WINDOW_EXPOSE";
- case Event.WINDOW_ICONIFY: return "WINDOW_ICONIFY";
- case Event.WINDOW_MOVED: return "WINDOW_MOVED";
- default: return "UNKNOWN_ID:" + id;
- }
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Create a human readable string corresponding to an event key code.
- * @param int key the key code.
- * @return String a string with the human readable name of the key code.
- */
- public static String key_string(int key)
- {
- switch (key) {
- case Event.DOWN: return "DOWN";
- case Event.END: return "END";
- case Event.F1: return "F1";
- case Event.F2: return "F2";
- case Event.F3: return "F3";
- case Event.F4: return "F4";
- case Event.F5: return "F5";
- case Event.F6: return "F6";
- case Event.F7: return "F7";
- case Event.F8: return "F8";
- case Event.F9: return "F9";
- case Event.F10: return "F10";
- case Event.F11: return "F11";
- case Event.F12: return "F12";
- case Event.HOME: return "HOME";
- case Event.LEFT: return "LEFT";
- case Event.PGDN: return "PGDN";
- case Event.PGUP: return "PGUP";
- case Event.RIGHT: return "RIGHT";
- case Event.UP: return "UP";
- default:
- if (key > 31 && key < 127)
- return "\"" + (char)key + "\"";
- else
- return "" + key;
- }
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Create a human readable string corresponding to an event modifier mask.
- * @param int mod the modifier mask.
- * @return String a string with the human readable dump of the modifier mask.
- */
- public static String mod_string(int mod)
- {
- StringBuffer result = new StringBuffer();
- if ((mod & Event.SHIFT_MASK) != 0) result.append("SHIFT ");
- if ((mod & Event.CTRL_MASK) != 0) result.append("CTRL ");
- if ((mod & Event.META_MASK) != 0) result.append("META ");
- if ((mod & Event.ALT_MASK) != 0) result.append("ALT");
- return result.toString();
- }
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Convert event to a human readable string which describes it. */
- public String toString()
- {
- StringBuffer result = new StringBuffer();
-
- result.append("sub_arctic.event[global_x,y=");
- result.append(global_x()); result.append(","); result.append(global_y());
- result.append(",localx,y=");
- result.append(local_x()); result.append(","); result.append(local_y());
- result.append(",id="); result.append(id_string(id()));
- result.append(",key="); result.append(key_string(key()));
- result.append(",mod="); result.append(mod_string(modifiers()));
- result.append("]");
-
- return result.toString();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /* constants copied from Event */
- public static final int SHIFT_MASK = Event.SHIFT_MASK;
- public static final int CTRL_MASK = Event.CTRL_MASK;
- public static final int META_MASK = Event.META_MASK;
- public static final int ALT_MASK = Event.ALT_MASK;
- public static final int HOME = Event.HOME;
- public static final int END = Event.END;
- public static final int PGUP = Event.PGUP;
- public static final int PGDN = Event.PGDN;
- public static final int UP = Event.UP;
- public static final int DOWN = Event.DOWN;
- public static final int LEFT = Event.LEFT;
- public static final int RIGHT = Event.RIGHT;
- public static final int F1 = Event.F1;
- public static final int F2 = Event.F2;
- public static final int F3 = Event.F3;
- public static final int F4 = Event.F4;
- public static final int F5 = Event.F5;
- public static final int F6 = Event.F6;
- public static final int F7 = Event.F7;
- public static final int F8 = Event.F8;
- public static final int F9 = Event.F9;
- public static final int F10 = Event.F10;
- public static final int F11 = Event.F11;
- public static final int F12 = Event.F12;
- public static final int WINDOW_DESTROY = Event.WINDOW_DESTROY;
- public static final int WINDOW_EXPOSE = Event.WINDOW_EXPOSE;
- public static final int WINDOW_ICONIFY = Event.WINDOW_ICONIFY;
- public static final int WINDOW_DEICONIFY = Event.WINDOW_DEICONIFY;
- public static final int WINDOW_MOVED = Event.WINDOW_MOVED;
- public static final int KEY_PRESS = Event.KEY_PRESS;
- public static final int KEY_RELEASE = Event.KEY_RELEASE;
- public static final int KEY_ACTION = Event.KEY_ACTION;
- public static final int KEY_ACTION_RELEASE = Event.KEY_ACTION_RELEASE;
- public static final int MOUSE_DOWN = Event.MOUSE_DOWN;
- public static final int MOUSE_UP = Event.MOUSE_UP;
- public static final int MOUSE_MOVE = Event.MOUSE_MOVE;
- public static final int MOUSE_ENTER = Event.MOUSE_ENTER;
- public static final int MOUSE_EXIT = Event.MOUSE_EXIT;
- public static final int MOUSE_DRAG = Event.MOUSE_DRAG;
- public static final int SCROLL_LINE_UP = Event.SCROLL_LINE_UP;
- public static final int SCROLL_LINE_DOWN = Event.SCROLL_LINE_DOWN;
- public static final int SCROLL_PAGE_UP = Event.SCROLL_PAGE_UP;
- public static final int SCROLL_PAGE_DOWN = Event.SCROLL_PAGE_DOWN;
- public static final int SCROLL_ABSOLUTE = Event.SCROLL_ABSOLUTE;
- public static final int LIST_SELECT = Event.LIST_SELECT;
- public static final int LIST_DESELECT = Event.LIST_DESELECT;
- public static final int ACTION_EVENT = Event.ACTION_EVENT;
- public static final int LOAD_FILE = Event.LOAD_FILE;
- public static final int SAVE_FILE = Event.SAVE_FILE;
- public static final int GOT_FOCUS = Event.GOT_FOCUS;
- public static final int LOST_FOCUS = Event.LOST_FOCUS;
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * AWT method:
- * Translates an event relative to the given component. This
- * involves at a minimum translating the coordinates so they make
- * sense within the given component. It may also involve
- * translating a region in the case of an expose event.<br>
- *
- * For subArctic this affects the global position of the event. (You rarely
- * want or need to do this). The local position is adjusted accordingly.
- *
- * @param x the x coordinate
- * @param y the y coordinate
- */
- public void translate(int x, int y)
- {
- _awt_event.translate(x,y);
- set_local_x(x + local_x());
- set_local_y(y + local_y());
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * AWT method:
- * Checks if the shift key is down.
- * @see #modifiers
- * @see #controlDown
- * @see #metaDown
- */
- public boolean shiftDown() {return _awt_event.shiftDown();}
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * AWT method:
- * Checks if the control key is down.
- * @see #modifiers
- * @see #shiftDown
- * @see #metaDown
- */
- public boolean controlDown() {return _awt_event.controlDown();}
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * AWT method:
- * Checks if the meta key is down.
- * @see #modifiers
- * @see #shiftDown
- * @see #controlDown
- */
- public boolean metaDown() {return _awt_event.metaDown();}
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
- }
- /*=========================== COPYRIGHT NOTICE ===========================
-
- This file is part of the subArctic user interface toolkit.
-
- Copyright (c) 1996 Scott Hudson and Ian Smith
- All rights reserved.
-
- The subArctic system is freely available for most uses under the terms
- and conditions described in
- http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html
- and appearing in full in the lib/interactor.java source file.
-
- The current release and additional information about this software can be
- found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
-
- ========================================================================*/
-